package com.ladlee.parking.controller;

import com.ladlee.parking.entity.vo.UserVO;
import com.ladlee.parking.mapper.SysUserMapper;
import com.ladlee.parking.pay.v3.core.PayV3Manager;
import com.ladlee.parking.pay.v3.enums.PayType;
import com.ladlee.parking.pay.v3.enums.WxTradeType;
import com.ladlee.parking.pay.v3.handle.PayHandler;
import com.ladlee.parking.pay.v3.model.Amount;
import com.ladlee.parking.pay.v3.model.Payer;
import com.ladlee.parking.pay.v3.model.UnifiedOrderModel;
import com.ladlee.parking.service.SysParkingService;
import com.ladlee.parking.util.R;
import com.ladlee.parking.validate.custom.EnumCheck;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/pay")
@Validated
public class PayController {

    @Autowired
    SysParkingService sysParkingService;

    @Autowired
    PayV3Manager payV3Manager;
    @Autowired
    SysUserMapper sysUserMapper;

    @GetMapping(value = "/pay")
    public R pay(@RequestParam Long parkingId,
                 @EnumCheck(enumClass = WxTradeType.class) @RequestParam String tradeType,
                 @EnumCheck(enumClass = PayType.class) @RequestParam String payType) throws Exception {
        PayHandler<UnifiedOrderModel> handler = payV3Manager.getHandler(payType, tradeType);
        if (handler == null) {
            throw new IllegalArgumentException("没有该支付类型的处理器");
        }
        UnifiedOrderModel unifiedOrderModel = new UnifiedOrderModel()
                .setAttach("这是测试")
                .setAmount(new Amount().setTotal(1))
                .setPayer(new Payer().setOpenid("123456"));
        String result = handler.handle(unifiedOrderModel);
        return R.ok(result);
    }

    @GetMapping(value = "/sensitive")
    public Object testSensitive() {
        return sysUserMapper.getUserVoByUsername("admin");
    }
}
